StringIterator

Section: Misc. Reference Manual Pages (3C++)
Updated: C++ String Library
Index Return to Main Contents
 

NAME

StringIterator - a character iterator for the String class.  

SYNOPSIS


# include <String.h>
   ...
  class StringIterator {
    public:
        StringIterator(const String &str);
        int operator()(char &ch);
        int pos();
   private:
        const String &s;
        int p;
  };
   ...
 

Constructors

StringIterator(const String &str)
 

DESCRIPTION

The constructor is used to declare a StringIterator variable. During the scope of the StringIterator variable, the value of the String should not change. If it is necessary to change the String, create a copy of the String by doing the following:


      StringIterator item(String(s));

Where 's' is the String you want to iterate over.  

OPERATOR ()

int operator()(char &ch)
 

DESCRIPTION

The () operator iterates over the String variable. It returns 0 when the end of the String is reached. Upon each invocation, the variable 'ch' will be set to the next character in the String.  

FUNCTION pos

int pos()
 

DESCRIPTION

The pos function returns the position of the last character returned from the () operator. If the () operator has not been called yet, then a -1 will be returned. If the end of the String has been reached, then pos will return the position just past the end of the String.

 

EXAMPLES


   String s("abc");
   StringIterator next(s);
   char ch;
   ...
   while (next(ch)) cout << ch << endl;
   ...


   This example would print each charcter in the String, one per
   line.


   whitespace(const String s, int &start, int &len) 
   {
      StringIterator next(s);
      char ch; 


      while (next(ch)) 
         if (isspace(ch)) {
              start=next.pos();
              while (next(ch) && isspace(ch));
              len=next.pos()-start;
              return;           }

      len = 0; start = -1;
   }


   This function will find the first occurence of whitespace within
   a given String, and set start to the first position, and len to the
   length of the whitespace.   


 

Index

NAME
SYNOPSIS
Constructors
DESCRIPTION
OPERATOR ()
DESCRIPTION
FUNCTION pos
DESCRIPTION
EXAMPLES

This document was created by man2html, using the manual pages.
Time: 00:37:36 GMT, March 30, 2022